package com.project.lk.nurseapp.utils;

import android.util.Log;


import com.project.lk.nurseapp.base.BaseApp;
import com.project.lk.nurseapp.db.UserDaoBean;
import com.project.lk.nurseapp.db.UserDaoBeanDao;

import org.greenrobot.greendao.query.Query;

import java.util.List;

/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class UserInfoUtils {
    private String username;
    private String birth;
    private String sex;
    private String headImg;
    private String accountId;
    private String nickName;
    private String baseUserInfoId;
    private String mobile;
    private String healthUrl;

    public UserInfoUtils() {


        //查询数据库
        UserDaoBeanDao userDaoBeanDao = BaseApp.getInstance().getDaoSession().getUserDaoBeanDao();

        if (userDaoBeanDao != null) {
            List<UserDaoBean> list = userDaoBeanDao.loadAll();
            if (list.size() != 0) {
                UserDaoBean userDaoBean = list.get(0);
                nickName =userDaoBean.getNickName();
                setNickName(nickName);
                birth = userDaoBean.getBirth();
                setBirth(birth);
                sex = userDaoBean.getSex();
                setSex(sex);
                 mobile = userDaoBean.getMobile();
                setMobile(mobile);
                baseUserInfoId = userDaoBean.getBaseUserInfoId();
                setBaseUserInfoId(baseUserInfoId);
                accountId = userDaoBean.getAccountId();
                setAccountId(accountId);
                headImg = userDaoBean.getHeadImg();
                setHeadImg(headImg);
                username=userDaoBean.getUsername();
                setUsername(username);
                 healthUrl = userDaoBean.getHealthUrl();
                setHealthUrl(healthUrl);
                //TODO:这里一定要写对，看清楚很容易写错set和get方法，不然会报空指针
//                Log.e("MLOG","accountId:"+accountId+"\nnickName:"+nickName+
//                        "\nbirth:"+birth+"\nmobile:"+mobile+"\nusername:"+username
//                    +"\nbaseUserInfoId:"+baseUserInfoId+"\nheadImg:"+headImg+"\nsex:"+sex);
            }
        }
    }

    public String getHealthUrl() {
        return healthUrl;
    }

    public void setHealthUrl(String healthUrl) {
        this.healthUrl = healthUrl;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getBirth() {
        return birth;
    }

    public void setBirth(String birth) {
        this.birth = birth;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getHeadImg() {
        return headImg;
    }

    public void setHeadImg(String headImg) {
        this.headImg = headImg;
    }

    public String getAccountId() {
        return accountId;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getBaseUserInfoId() {
        return baseUserInfoId;
    }

    public void setBaseUserInfoId(String baseUserInfoId) {
        this.baseUserInfoId = baseUserInfoId;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    /**
     * 根据查询条件,返回数据列表
     * @param where        条件
     * @param params       参数
     * @return             数据列表
     */
    public List<UserDaoBean> queryN(String where, String... params){
        return getUserDao().queryRaw(where, params);
    }

    /**
     * 根据用户信息,插件或修改信息
     * @param user 用户信息
     * @return 插件或修改的用户id
     */
    public static long saveNote(UserDaoBean user){
        return getUserDao().insertOrReplace(user);
    }

    /**
     * 批量插入或修改用户信息
     * @param list      用户信息列表
     */
    public void saveNLists(final List<UserDaoBean> list){
        if(list == null || list.isEmpty()){
            return;
        }
        getUserDao().getSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for(int i=0; i<list.size(); i++){
                    UserDaoBean user = list.get(i);
                    getUserDao().insertOrReplace(user);
                }
            }
        });

    }

    /**
     * 删除所有数据
     */
    public static void deleteAllNote(){
        getUserDao().deleteAll();
    }

    /**
     * 根据用户类,删除信息
     * @param user    用户信息类
     */
    public void deleteNote(UserDaoBean user){
        getUserDao().delete(user);
    }
    private static UserDaoBeanDao getUserDao() {
        return BaseApp.getInstance().getDaoSession().getUserDaoBeanDao();
    }
    /**
     * 从本地缓存中获取user对象
     *
     * @param Id ID
     * @return 返回用户信息
     */
    public static UserDaoBean getUserInfoFromCache(String id) {
        UserDaoBeanDao userDao = BaseApp.getInstance().getDaoSession().getUserDaoBeanDao();
        Query<UserDaoBean> query = userDao.queryBuilder().where(UserDaoBeanDao.Properties.Id.eq(id))
                .build();
        return query.unique();
    }
}
